package cn.bluech.lib.utils;

import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import cn.bluech.lib.http.SSLFactory;

public class NetUtils {
    /**
     * 测试是否可以访问网络
     * @param url 测试地址
     */
    public static boolean isAvailable(String url){
        HttpsURLConnection con = null ;

        try {
            con = (HttpsURLConnection) new URL(url)
                    .openConnection();
            con.setConnectTimeout(3000);
            if(url.startsWith("https"))
                con.setSSLSocketFactory(SSLFactory.ALL_TRUST);

            return (con.getResponseCode() == 200);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception ignored){
        }finally{
            try{
                con.disconnect();
            }catch(Exception ignored){ }
        }

        return false;
    }

    public static boolean isAvailable(){
        return isAvailable("https://www.baidu.com");
    }
}
